// ==UserScript==
// @name         SXKJ
// @namespace    http://tampermonkey.net/
// @version      1.0.20241210
// @description  try to take over the world!
// @author       You
// @match        think-will.cn/sxkjapi/*
// @match        *.think-will.cn/sxkjapi/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=think-will.cn
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const $ = ($) => document.querySelector($)
  console.log('=============> sxkj')

  // 生成一个点击按钮
  const btn = document.createElement('button')
  btn.innerText = '复制小程序api'
  btn.style.position = 'fixed'
  btn.style.top = '10px'
  btn.style.right = '10px'
  btn.style.zIndex = '9999'
  document.body.appendChild(btn)

  btn.addEventListener('click', () => {
    copyFn()

  })

  function copyFn () {
    let api = $('.ant-tabs-tabpane-active .ant-tabs-tabpane-active .knife4j-api-summary-path').innerText
    let apiFnName = api.split('/')[3]
    let apiName = $('.ant-tabs-tabpane-active .ant-tabs-tabpane-active .knife4j-api-title span').innerText
    let apiMethod = $('.ant-tabs-tabpane-active .ant-tabs-tabpane-active .knife4j-api-summary-method').innerText
    apiMethod = apiMethod.toLowerCase()
    let endFnName = api.split('/')[3]
    if (endFnName.includes('PageItems')) {
      apiFnName = endFnName.split('PageItems')[0] + 'List'
    } else if (endFnName.includes("PageItem")) {
      apiFnName = endFnName.split('PageItem')[0] + 'Detail'
    }

    apiFnName = apiFnName[0].toLowerCase() + apiFnName.slice(1)

    if (apiFnName.startsWith('get')) {
      apiName = '获取' + apiName
    }

    const res = `
/**
 * ${apiName}
 * @param {object} params - 请求数据
 * @param {object} opts - 配置
 * @return {Promise<Array>}  err, res
 */
export async function ${apiFnName}Api (params, opts) {
  return await ${'$' + apiMethod}('${api}', params, {
    err_tips: '${apiName}失败',
    ...opts,
  })
}
`
    navigator.clipboard.writeText(res)
    btn.innerText = '复制成功'
    setTimeout(() => {
      btn.innerText = '复制小程序api'
    }, 1000)
  }

})();